<template>
	<el-main class="bg">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
			<el-col v-if="user_group === '管理员' || $check_field('get','patient_number') || $check_field('add','patient_number') || $check_field('set','patient_number')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="患者编号" prop="patient_number">
					<el-input id="patient_number" v-model="form['patient_number']" placeholder="请输入患者编号"
							  v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','patient_number')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','patient_number'))" :disabled="disabledObj['patient_number_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','patient_number')">{{form['patient_number']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','patient_name') || $check_field('add','patient_name') || $check_field('set','patient_name')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="患者姓名" prop="patient_name">
					<el-input id="patient_name" v-model="form['patient_name']" placeholder="请输入患者姓名"
							  v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','patient_name')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','patient_name'))" :disabled="disabledObj['patient_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','patient_name')">{{form['patient_name']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','age') || $check_field('add','age') || $check_field('set','age')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="年龄" prop="age">
					<el-input id="age" v-model="form['age']" placeholder="请输入年龄"
							  v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','age')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','age'))" :disabled="disabledObj['age_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','age')">{{form['age']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','gender') || $check_field('add','gender') || $check_field('set','gender')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="性别" prop="gender">
					<el-input id="gender" v-model="form['gender']" placeholder="请输入性别"
							  v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','gender')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','gender'))" :disabled="disabledObj['gender_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','gender')">{{form['gender']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','id') || $check_field('add','id') || $check_field('set','id')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="身份证" prop="id">
					<el-input id="id" v-model="form['id']" placeholder="请输入身份证"
							  v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','id')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','id'))" :disabled="disabledObj['id_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','id')">{{form['id']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','patient') || $check_field('add','patient') || $check_field('set','patient')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="患者" prop="patient">
						<el-select v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','patient')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','patient'))" id="patient" v-model="form['patient']" :disabled="disabledObj['patient_isDisabled']">
							<el-option v-for="o in list_user_patient" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
						<el-select v-else-if="$check_field('get','patient')" id="patient" v-model="form['patient']" :disabled="true">
							<el-option v-for="o in list_user_patient" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','whether_hospitalized') || $check_field('add','whether_hospitalized') || $check_field('set','whether_hospitalized')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="是否住院" prop="whether_hospitalized">
					<el-select id="whether_hospitalized" v-model="form['whether_hospitalized']"
						v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','whether_hospitalized')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','whether_hospitalized'))">
						<el-option v-for="o in list_whether_hospitalized" :key="o" :label="o" :value="o">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','whether_hospitalized')">{{form['whether_hospitalized']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','doctor') || $check_field('add','doctor') || $check_field('set','doctor')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="医生" prop="doctor">
						<div v-if="user_group !== '管理员'">
							{{ get_user_session_doctor(form['doctor']) }}
							<!--<el-input id="business_name" v-model="form['doctor']" placeholder="请输入医生"-->
							<!--v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','doctor')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','doctor'))" :disabled="disabledObj['doctor_isDisabled']"></el-input>-->
							<!--<div v-else-if="$check_field('get','doctor')">{{form['doctor']}}</div>-->
							<el-select v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','doctor')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','doctor'))" id="doctor" v-model="form['doctor']" :disabled="disabledObj['doctor_isDisabled']">
								<el-option v-for="o in list_user_doctor" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','doctor')" id="doctor" v-model="form['doctor']" :disabled="true">
								<el-option v-for="o in list_user_doctor" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="doctor" v-model="form['doctor']" :disabled="disabledObj['doctor_isDisabled']">
							<el-option v-for="o in list_user_doctor" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','diagnosis_and_treatment_results') || $check_field('add','diagnosis_and_treatment_results') || $check_field('set','diagnosis_and_treatment_results')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="诊疗结果" prop="diagnosis_and_treatment_results">
					<el-input type="textarea" id="diagnosis_and_treatment_results" v-model="form['diagnosis_and_treatment_results']" placeholder="请输入诊疗结果"
						v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','diagnosis_and_treatment_results')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','diagnosis_and_treatment_results'))" :disabled="disabledObj['diagnosis_and_treatment_results_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','diagnosis_and_treatment_results')">{{form['diagnosis_and_treatment_results']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','cause_original') || $check_field('add','cause_original') || $check_field('set','cause_original')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="主要原因" prop="cause_original">
					<el-input type="textarea" id="cause_original" v-model="form['cause_original']" placeholder="请输入主要原因"
						v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','cause_original')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','cause_original'))" :disabled="disabledObj['cause_original_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','cause_original')">{{form['cause_original']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','prescription') || $check_field('add','prescription') || $check_field('set','prescription')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="处方药" prop="prescription">
					<el-input type="textarea" id="prescription" v-model="form['prescription']" placeholder="请输入处方药"
						v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','prescription')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','prescription'))" :disabled="disabledObj['prescription_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','prescription')">{{form['prescription']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','medical_advice') || $check_field('add','medical_advice') || $check_field('set','medical_advice')" :xs="24" :sm="12" :lg="8">
				<el-form-item label="医嘱" prop="medical_advice">
					<el-input type="textarea" id="medical_advice" v-model="form['medical_advice']" placeholder="请输入医嘱"
						v-if="user_group === '管理员' || (form['diagnosis_and_treatment_results_id'] && $check_field('set','medical_advice')) || (!form['diagnosis_and_treatment_results_id'] && $check_field('add','medical_advice'))" :disabled="disabledObj['medical_advice_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','medical_advice')">{{form['medical_advice']}}</div>
				</el-form-item>
			</el-col>
			<el-col :xs="24" :sm="12" :lg="8">
				<el-form-item>
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				field: "diagnosis_and_treatment_results_id",
				url_add: "~/api/diagnosis_and_treatment_results/add?",
				url_set: "~/api/diagnosis_and_treatment_results/set?",
				url_get_obj: "~/api/diagnosis_and_treatment_results/get_obj?",
				url_upload: "~/api/diagnosis_and_treatment_results/upload?",

				query: {
					"diagnosis_and_treatment_results_id": 0,
				},

				form: {
					"patient_number":'', // 患者编号
					"patient_name":'', // 患者姓名
					"age":'', // 年龄
					"gender":'', // 性别
					"id":'', // 身份证
					"patient": 0, // 患者
					"whether_hospitalized":'', // 是否住院
					"doctor": 0, // 医生
					"diagnosis_and_treatment_results":'', // 诊疗结果
					"cause_original":'', // 主要原因
					"prescription":'', // 处方药
					"medical_advice":'', // 医嘱
					"diagnosis_and_treatment_results_id": 0, // ID

				},
				disabledObj:{
					"patient_number_isDisabled": false,
					"patient_name_isDisabled": false,
					"age_isDisabled": false,
					"gender_isDisabled": false,
					"id_isDisabled": false,
					"patient_isDisabled": false,
					"whether_hospitalized_isDisabled": false,
					"doctor_isDisabled": false,
					"diagnosis_and_treatment_results_isDisabled": false,
					"cause_original_isDisabled": false,
					"prescription_isDisabled": false,
					"medical_advice_isDisabled": false,
				},
				// 用户列表
				list_user_patient: [],
				//是否住院选项列表
				list_whether_hospitalized: ['是','否'],
				// 用户列表
				list_user_doctor: [],
				// 用户组
				group_user_doctor: "",

			}
		},
		methods: {
			/**
			 * 获取患者用户列表
			 */
			async get_list_user_patient() {
                // if(this.user_group !== "管理员" && this.form["patient"] === 0) {
                //     this.form["patient"] = this.user.user_id;
                // }
                var json = await this.$get("~/api/user/get_list?user_group=患者");
                if(json.result && json.result.list){
                    this.list_user_patient = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
			get_user_patient(id){
				var obj = this.list_user_patient.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			/**
			 * 获取医生用户列表
			 */
			async get_list_user_doctor() {
                // if(this.user_group !== "管理员" && this.form["doctor"] === 0) {
                //     this.form["doctor"] = this.user.user_id;
                // }
                var json = await this.$get("~/api/user/get_list?user_group=医生");
                if(json.result && json.result.list){
                    this.list_user_doctor = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
			/**
			 * 获取医生用户组
			 */
			async get_group_user_doctor() {
				this.form["doctor"] = this.user.user_id;
				var json = await this.$get("~/api/user_group/get_obj?name=医生");
				if(json.result && json.result.obj){
					this.group_user_doctor = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_doctor(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_doctor.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
						for (let key in _this.form) {
							arrForm.push(key)
						}
						for (var i=0;i<arr.length;i++){
							for (var j=0;j<arrForm.length;j++){
								if (arr[i]===arrForm[j]){
									if (arr[i]!=="doctor") {
										_this.form[arrForm[j]] = res.result.obj[arr[i]]
										_this.disabledObj[arrForm[j] + '_isDisabled'] = true
										break;
									}else {
										_this.disabledObj[arrForm[j] + '_isDisabled'] = true
									}
								}
							}
						}
					}
				});
			},
			get_user_doctor(id){
				var obj = this.list_user_doctor.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},

			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
				// 获取缓存数据附加
				form = $.db.get("form");
				$.push(this.form ,form);
				/**
				* 请求列表前
				* @param {Object} param
				*/
				var user_group = this.user.user_group;
				if (user_group !== "管理员") {
					switch (user_group) {
						case "患者":
							if(param["patient"] > 0){
								param["patient"] = this.user.user_id;
							}
							break;
						case "医生":
							if(param["doctor"] > 0){
								param["doctor"] = this.user.user_id;
							}
							break;
					}
				}
				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
							}
						})
					})
				}
				$.db.del("form");
				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){

			},

			is_view(){
				var bl = this.user_group == "管理员";

				if(!bl){
					bl = this.$check_action('/diagnosis_and_treatment_results/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/diagnosis_and_treatment_results/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/diagnosis_and_treatment_results/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/diagnosis_and_treatment_results/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/diagnosis_and_treatment_results/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
			this.get_list_user_patient();
			this.get_list_user_doctor();
			this.get_group_user_doctor();
		}
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
